/**
 * 一般在容器中做这4件事
 *    1. 定义容器并导出
 *    2. 使用容器中的state
 *    3. 修改容器中的state
 *    4. 使用容器中的action
 */
import { defineStore } from "pinia";
import { computed, ref,reactive} from 'vue'
import {userInfo,codeInfo,JudgeResponse,JudgeCase,JudgeCaseResult} from "@/pojo/entity"
import {creatWebSocket,sendWebSocket,closeWebSocket} from '@/util/websocket'
import BaseUrl from '@/config/BaseUrl.json'
import {Base64} from 'js-base64'
import { keysOf } from "naive-ui/es/_utils";
/**
 * 1. 定义容器并导出
 * 参数一: 容器ID, 唯一, 将来 Pinia 会把所有的容器挂载到根容器
 * 参数二: 选项对象
 * 返回值: 函数, 调用的时候要空参调用, 返回容器实例
 */
// 设置初始化，防止刷新时恢复默认

  
 const mainStore = defineStore('main',()=> {
    const codeInfo=reactive<codeInfo>({
        SubmissionID:'',
        problemID:'1',
        sourceCode:'', /**代码内容 */
        language:'python', /**编程语言 */
        stdin:'', /**输入 */
        expectedOutput:'' /**期望输出 */
    })

    //存储样例数据

    const JudgeCase=reactive<JudgeCase>({
        SubmissionID:'',
        problemID:'1',
        sourceCode:'', /**代码内容 */
        language:'python', /**编程语言 */
        stdin:'', /**输入 */
        expectedOutput:'', /**期望输出 */
        caseStdin: new Map<string,string>()
    })
    //存储判题记录
    const totalJudgeResponseArray:JudgeResponse[]=reactive([])

    const JudgeCaseResultMap:Map<string, JudgeCaseResult[]>=reactive(new Map<string, JudgeCaseResult[]>());
    
    //存放样例数据
    
    
    //存储判题结果
    const totalJudgeResponse=reactive<JudgeResponse>({} as JudgeResponse)

     /**
         * 运行代码
         */
     const SubmitCode =(language:string)=> {
        creatWebSocket(BaseUrl.WEBSOCKET_URL+"/websocket/SubmitCode/123")
        codeInfo['language']=   language === '' ? 'python':language
        sendWebSocket(codeInfo) 
     }
    /**
     * 运行代码 
     */
   const RunCode =()=> {
        creatWebSocket(BaseUrl.WEBSOCKET_URL+"/websocket/RunCode/123")
        JudgeCase.problemID=codeInfo.problemID;
        JudgeCase.sourceCode=codeInfo.sourceCode;
        JudgeCase.language=codeInfo.language; 
        
        sendWebSocket(JudgeCase) 
    }

   


    return{
        codeInfo,
        JudgeCase,
        totalJudgeResponseArray,
        totalJudgeResponse,
        RunCode,
        SubmitCode,
        JudgeCaseResultMap
    }
    
})

const session = defineStore('session',()=>{
     // 定义数据
     const token = ref<string|any>("")
     const userInfo=reactive<userInfo|any>(null)
    
     // 定义计算属性
     const counterComputer = computed(() => {
        return 0;
     })
     
     // 定义方法,登录
     const login = () => {
         return 0;
     }
     //获取token
     const get=(property:string)=>{
        if(property==="token")
        return token;
     }
     return {
         token,
         login,
         get,
         counterComputer
     }
 
})
export {mainStore,session}